package com.yequan.hbb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.entries.TypeListEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/15.
 */

public class TypeAdapter  extends RecyclerView.Adapter<TypeAdapter.TypeViewHolder> {


    private List<TypeListEntry.DataBean.ClassifyBean> mClassifyBeanList;
    private Context  context;
    private DetailAdapter mDetailAdapter;

    private  View lastView;
    public TypeAdapter(Context context,DetailAdapter detailAdapter) {
        this.context = context;
        this.mDetailAdapter=detailAdapter;
        mClassifyBeanList=new ArrayList<>();
    }

    public void addAll(List<TypeListEntry.DataBean.ClassifyBean> lassifyBeanList){
        if (lassifyBeanList==null)return ;
        mClassifyBeanList.addAll(lassifyBeanList);
        notifyDataSetChanged();
    }

    public void add(TypeListEntry.DataBean.ClassifyBean d){
        mClassifyBeanList.add(d);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.wl_type_left;
    }

    @Override
    public TypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(viewType, parent,false);
        return new TypeViewHolder(inflate);
    }



    @Override
    public void onBindViewHolder(final TypeViewHolder holder, int position) {


        final TypeListEntry.DataBean.ClassifyBean classifyBean = mClassifyBeanList.get(position);
        holder.type_left.setText(classifyBean.getName());
        if (position==0){
            holder.iv_left.setVisibility(View.VISIBLE);
            lastView=holder.itemView;
            mDetailAdapter.addAll(classifyBean.getList());
        }
        if (!holder.type_left.hasOnClickListeners()) {
            holder.type_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lastView.findViewById(R.id.iv_check).setVisibility(View.INVISIBLE);
                    holder.iv_left.setVisibility(View.VISIBLE);
                    lastView=holder.itemView;
                    mDetailAdapter.addAll(classifyBean.getList());
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return mClassifyBeanList.size();
    }

    public static class TypeViewHolder extends RecyclerView.ViewHolder{

        private final ImageView iv_left;
        private final TextView type_left;

        public TypeViewHolder(View itemView) {
            super(itemView);
            iv_left = ((ImageView) itemView.findViewById(R.id.iv_check));
            type_left = ((TextView) itemView.findViewById(R.id.type_left));
        }
    }
}
